package com.example.user1.service.impl;

import com.example.user1.enums.VerifyCodeTypeEnum;
import com.example.user1.util.RedisImageCaptchaUtil;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@RequiredArgsConstructor

public class VerifyCodeImageServiceImpl extends VerifyCodeServiceImpl {
    private final RedisImageCaptchaUtil redisImageCaptchaUtil;
    private final static Integer codeSize = 4;

    @Override
    public void generate(String clientId) {
        redisImageCaptchaUtil.generator(Long.valueOf(clientId), codeSize);
    }

    public boolean match(@NotNull String clientId, @NotEmpty String code) {
        if (code.length() != codeSize) {
            return false;
        }
        String existCode = redisImageCaptchaUtil.getCaptcha(Long.valueOf(clientId));
        return StringUtils.hasText(existCode) && existCode.equals(code);
    }

    @Override
    public VerifyCodeTypeEnum type() {
        return VerifyCodeTypeEnum.IMAGE;
    }
}
